//
//  ChatViewController.m
//  Exam16-1
//
//  Created by 孙晓晔 on 2017/5/28.
//  Copyright © 2017年 孙晓晔. All rights reserved.
//

#import "ChatViewController.h"

@interface ChatViewController ()<EMChatManagerDelegate>
@property (weak, nonatomic) IBOutlet UITextField *inputText;

@property(nonatomic,strong) EMConversation *conversation;

@end

@implementation ChatViewController


-(instancetype)initWithConversationId:(NSString*)conversationId conversationType:(EMConversationType)conversationType {
    
    self = [super initWithNibName:@"ChatViewController" bundle:nil];
    if (self) {
        _conversation = [[EMClient sharedClient].chatManager getConversation:conversationId type:conversationType createIfNotExist:YES];
        
        [[EMClient sharedClient].chatManager addDelegate:self delegateQueue:nil];
    }
    
    return self;
}

-(void)dealloc {
    [[EMClient sharedClient].chatManager removeDelegate:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)send:(id)sender {
    EMTextMessageBody *body = [[EMTextMessageBody alloc] initWithText:_inputText.text];
    
    EMMessage *message = [[EMMessage alloc] initWithConversationID:_conversation.conversationId from:[EMClient sharedClient].currentUsername to:_conversation.conversationId body:body ext:nil];
    
    [[EMClient sharedClient].chatManager sendMessage:message progress:nil completion:^(EMMessage *message, EMError *error) {
        if (!error) {
            NSLog(@"发送成功");
        }
    }];
}

-(void)messagesDidReceive:(NSArray *)aMessages {
    
    NSLog(@"接收消息 %ld 条", aMessages.count);
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
